package com.hsc.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsc.eduservice.entity.EduCourse;
import com.hsc.eduservice.entity.EduCourseDescription;
import com.hsc.eduservice.entity.frontvo.CourseFrontVo;
import com.hsc.eduservice.entity.frontvo.CourseWebVo;
import com.hsc.eduservice.entity.vo.CourseInfoVo;
import com.hsc.eduservice.entity.vo.CoursePublishVo;
import com.hsc.eduservice.mapper.EduCourseMapper;
import com.hsc.eduservice.service.EduChapterService;
import com.hsc.eduservice.service.EduCourseDescriptionService;
import com.hsc.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsc.eduservice.service.EduVideoService;
import com.hsc.servicebase.exceptionHandle.GuliException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-10-13
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //课程简介描述注入
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    //注入小节service
    @Autowired
    private EduVideoService videoService;
    //注入章节service
    private EduChapterService chapterService;

    @Override
    public String saveCouserInfo(CourseInfoVo courseInfoVo) {
        //向课程表添加课程基本信息
        //CourseInfoVo转换成eduCourse
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert==0){
            throw new GuliException(20001,"添加课程表数据异常");
        }

        String cid = eduCourse.getId();
        //向课程表添加课程简介
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);
        return cid;

    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        //查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //查询描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update==0){
            throw new GuliException(20001,"课程修改信息失败");
        }
        //修改描述表
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(courseInfoVo.getId());
        courseDescription.setDescription(courseInfoVo.getDescription());
        boolean flag = courseDescriptionService.updateById(courseDescription);
        if(flag==false){
            throw new GuliException(20001,"课程描述信息修改失败");
        }
    }

    //根据id查询课程确认信息
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //调用mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    //删除课程信息
    @Override
    public void removeCourse(String courseId) {
        //根据课程id删除小节/一对多关系
        videoService.removeVideoByCourseId(courseId);
        //根据课程id删除章节/一对多关系
        chapterService.removeChapterByCourseId(courseId);
        //根据课程id删除秒速/一对一关系
        courseDescriptionService.removeById(courseId);
        //根据课程id删除课程本身
        int i = baseMapper.deleteById(courseId);
        if( i==0){
            throw new GuliException(20001,"删除失败");
        }
    }

    //条件查询带分页
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> eduCoursePage, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空则拼接
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){//判断一级分类是否为空
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){//判断二级分类是否为空
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){//关注度,有值则排序
            wrapper.orderByDesc("buy_count"); //降序，关注度越高，课程展示越靠前
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){//上架时间
            wrapper.orderByDesc("gmt_create"); //降序，最新上架的课程靠前
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){//价格排序
            wrapper.orderByDesc("price"); //最贵的靠前
        }


        baseMapper.selectPage(eduCoursePage,wrapper);

        //page数据信息提取
        List<EduCourse> records = eduCoursePage.getRecords();
        long current = eduCoursePage.getCurrent();
        long pages = eduCoursePage.getPages();
        long size = eduCoursePage.getSize();
        long total = eduCoursePage.getTotal();
        boolean hasNext = eduCoursePage.hasNext();//下一页
        boolean hasPrevious = eduCoursePage.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);


        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {

        return baseMapper.getBaseCourseInfo(courseId);
    }
}
